# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# 2.0.7 - This version does NOT support Python 2.x so we have to have
# a separate package for django 1.11.13 for Python2.
#
_realname=django
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.0.5
pkgrel=1
pkgdesc="A high-level Python Web framework that encourages rapid development and clean design (mingw-w64)"
arch=('any')
url='https://www.djangoproject.com/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-asgiref"
         "${MINGW_PACKAGE_PREFIX}-python-pytz"
         "${MINGW_PACKAGE_PREFIX}-python-sqlparse")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-psycopg2: for PostgreSQL backend"
            "${MINGW_PACKAGE_PREFIX}-python-bcrypt: for bcrypt support"
            "${MINGW_PACKAGE_PREFIX}-python-argon2_cffi: for Argon2 support")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-bcrypt"
             "${MINGW_PACKAGE_PREFIX}-python-argon2_cffi"
             "${MINGW_PACKAGE_PREFIX}-python-psycopg2"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' 'strip' '!debug')
install=${_realname}3-${CARCH}.install
source=(#"Django-${pkgver}.tar.gz"::"https://www.djangoproject.com/download/${pkgver}/tarball/"
        "${_realname}-${pkgver}.tar.gz"::https://github.com/django/django/archive/${pkgver}.tar.gz)
sha512sums=('92296ed8aabb7ea9ff22b15a99062c54ef8108f1429b947ca7f2ff482f2d04ddc74878721c9f99ecd123f299b7e46fcce3e464c8b35432cfa79c48f6f3c2ca6b')
noextract=(${_realname}-${pkgver}.tar.gz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|${MINGW_PREFIX}/bin/|g" -i ${_f}
  done
}
